package xing.repository.isv.suite.custom;

import org.springframework.transaction.annotation.Transactional;
import xing.domain.isv.suite.CorpSuiteAuth;
import xing.domain.isv.suite.QCorpSuiteAuth;
import xing.repository.BaseRepository;

import java.sql.Timestamp;

/*******************************************************************************************
 * 类描述：  
 *
 * @author: dustin
 * @date： 2017-8-22 11:18
 * @Description: 一句话描述该类的功能
 * @version 1.0.0
 *
 *
 * Version    Date       ModifiedBy                 Content  
 * -------- ---------    ----------         ------------------------  
 * 1.0.0     2017-8-22    Administrator                            
 *******************************************************************************************
 */
public class CorpSuiteAuthRepositoryImpl extends BaseRepository implements CorpSuiteAuthRepositoryCus {

    @Override
    @Transactional
    public int addOrUpdateSuiteAuth(CorpSuiteAuth cs) {
        QCorpSuiteAuth qCorpSuiteAuth=QCorpSuiteAuth.corpSuiteAuth;
        Long id=
        queryFactory.select(qCorpSuiteAuth.id)
                .from(qCorpSuiteAuth)
                .where(qCorpSuiteAuth.corpId.eq(cs.getCorpId()).and(qCorpSuiteAuth.suiteKey.eq(cs.getSuiteKey())))
                .fetchOne();

        if (id == null || id.longValue()==0) {
            entityManager.persist(cs);
            entityManager.flush();
            entityManager.clear();
            return 1;
        }else{
            long i=
            queryFactory.update(qCorpSuiteAuth)
                    .where(qCorpSuiteAuth.corpId.eq(cs.getCorpId()).and(qCorpSuiteAuth.suiteKey.eq(cs.getSuiteKey())))
                    .set(qCorpSuiteAuth.gmtModified,new Timestamp(System.currentTimeMillis()))
                    .set(qCorpSuiteAuth.permanentCode,cs.getPermanentCode())
                    .execute();
            return (int)i;
        }
        //long i=queryFactory.update(qCorpSuiteAuth).where(qCorpSuiteAuth.corpId)
    }
}
